<template>
  <div class="box">
    <span class="header">短信验证</span>
    <van-form class="yanzheng" ref="form">
      <van-field  placeholder="请输入手机号" name="mobile" v-model="form.mobile" :rules="rules.mobile" />
      <van-field  placeholder="请输入验证码" name="code" v-model="form.code" :rules="rules.code">
        <template #button >
          <span class="send" @click="send">
            {{ second === 0? '发送验证码' : `${second}秒后发送` }}
          </span>
        </template>
      </van-field>

      <van-button class="btn" @click="login" native-type="submit">
        <span class="btntext">登录</span>
      </van-button>
    </van-form>
  </div>
</template>

<script>
import { userLogin, userCode } from '@/api/login'
export default {
  name: 'LoginPage',
  data() {
    return {
      second: 0,
      timer: null,
      form: {
        mobile: '13212345678',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入电话号' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的电话号' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码' }
        ]
      }
    }
  },
  methods: {
    // 默认登录
    login () {
      this.$refs.form.validate().then(async() => {
        const res = await userLogin(this.form)
        this.$toast.success('登录成功')
        localStorage.setItem('jike', res.data.data.token)
      })
    },
    // 发送验证码登录
    async send () {
      // 时间大于0直接返回
      if (this.second > 0) return
      // 校验mobile
      await this.$refs.form.validate('mobile')
      // 发送短信验证
      const res = await userCode(this.form.mobile)
      console.log(res)
      // 失败提示
      if (!res) return this.$toast.fail('发送失败')
      // 成功则进入60秒倒计时
      this.second = 60
      if (this.timer) clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.second--
        if (this.second <= 0) clearInterval(this.timer)
      }, 1000)
    }
  },
  beforeDestroy () {
    if (this.timer) clearInterval(this.timer)
  }
}
</script>

<style lang="less" scoped>
.box {
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  margin-top: 60px;
  // height: 20vh;
  width: 100;
  .yanzheng {
    margin-top: 10px;
    display: flex;
    height: 200px;
    flex-direction: column;
    // justify-content: space-around;
  }
  .send {
    font-size: 12px;
    color: #fc6627;
  }
  .header {
    width: 200px;
    height: 34px;
    font-size: 28px;
    color: #333;
    font-weight: 600;
    margin-bottom: 40px;
  }
  .van-field {
    height: 60px;
    font-size: 16px;
    // line-height: 100px;
    // margin-top: 20px;
  }
  .btn {
    height: 50px;
    background-image: linear-gradient(-45deg, #fe4f4f 0%, #fc6627 100%);
    border-radius: 8px;
    .btntext {
      display: block;
      width: 64px;
      height: 32px;
      color: white;
      font-size: 16px;
      letter-spacing: 10px;
      text-align: center;
      line-height: 32px;
      font-weight: 400;
    }
  }
}
</style>
